package com.example.guiguiapplicatio.fragment.examination;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.guiguiapplicatio.api.GetExaminationRecordApi;
import com.example.guiguiapplicatio.api.GetExaminationStatusApi;
import com.example.guiguiapplicatio.api.Interface.GetExaminationRecordCallback;
import com.example.guiguiapplicatio.api.Interface.GetExaminationStatusCallback;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.ExaminationRecord;
import com.example.guiguiapplicatio.databinding.FragmentExaminationPersonBinding;
import com.example.guiguiapplicatio.fragment.examination.adapter.ExaminationPersonAdapter;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationPersonPlaceholderContent;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationPersonPlaceholderContent.PlaceholderItem;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class ExaminationPersonFragment extends Fragment implements GetExaminationRecordCallback, GetExaminationStatusCallback {
    FragmentExaminationPersonBinding binding;
    ExaminationPersonAdapter adapter;
    public static List<PlaceholderItem> NOW_ITEMS = new ArrayList<PlaceholderItem>();
    private GetExaminationRecordApi getExaminationRecordApi;
    private ArrayList<String> listenerNames = new ArrayList<>();
    private GetExaminationStatusApi getExaminationStatusApi;
    private static final String ARG_ID_CARD = "idCard";
    private String idCard;

    private String status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            idCard = getArguments().getString(ARG_ID_CARD);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentExaminationPersonBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        binding.examinationPersonList.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        NOW_ITEMS = ExaminationPersonPlaceholderContent.ITEMS;
        adapter = new ExaminationPersonAdapter(NOW_ITEMS, getContext());
        binding.examinationPersonList.setAdapter(adapter);
        Log.d("info", "身份证号idcard:" + idCard);
        getExaminationStatusApi = new GetExaminationStatusApi();
        getExaminationStatusApi.setGetExaminationStatusCallback(this);
        getExaminationStatusApi.performExaminationStatus(idCard);

        getExaminationRecordApi = new GetExaminationRecordApi(this);
        getExaminationRecordApi.setGetExaminationRecordCallback(this);


        getExaminationRecordApi.performGetExaminationRecord(idCard, -1, listenerNames);


        binding.servicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.popBackStack();
            }
        });
        return view;
    }


    @Override
    public void onERSuccess(ResponseBody<List<ExaminationRecord>> responseBody) {
        NOW_ITEMS.clear();
            Object data = responseBody.getData();
            if (data instanceof List) {
                List<ExaminationRecord> examinationRecords = (List<ExaminationRecord>) data;
                if (status.equals("1")) {
                if(examinationRecords.isEmpty()){
                    binding.examinationTitel.setText("无体检记录");
                }else{
                    Log.d("info", "体tt检记ttttt录涛涛涛涛数据：" + examinationRecords.toString());
                    binding.examinationTitel.setText("体检记录");

                }

            } else {
                    Log.d("info", "未签约");
                }
                for (ExaminationRecord examinationRecord : examinationRecords) {
                    ExaminationPersonPlaceholderContent.addItem(new ExaminationPersonPlaceholderContent.PlaceholderItem(
                            examinationRecord.getExaminationid(),
                            examinationRecord.getIdcard(),
                            examinationRecord.getName(),
                            examinationRecord.getSex(),
                            examinationRecord.getAge(),
                            examinationRecord.getStarttime(),
                            examinationRecord.getListenername()
                    ));
                }
                adapter.notifyDataSetChanged();

        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "体检记录响应成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onERFailure(String errorMessage) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "体检记录响应失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStatusSuccess(ResponseBody<Object> responseBody) {
        Log.d("info", "响应的体检数据数据：" + responseBody.toString());
        status = responseBody.getMsg();
        if (status.equals("0")) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    binding.examinationTitel.setText("未签约，无体检记录");
                    Toast.makeText(getContext(), "未签约，请先签约体检后查看。", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onStatusFailure(String errorMessage) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "签约状态响应失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);

        super.onResume();
    }
}
